package com.shoe.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shoe.mapper.UserLogisticMapper;
import com.shoe.service.UserLogisticService;
import com.shoe.util.DateConversion;
import com.shoe.util.ResultUtil;
import com.shoe.vo.ResponseResult;
import com.shoe.vo.UserlogisticVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserLogisticServiceImpl implements UserLogisticService {

    @Autowired
    ResultUtil resultUtil;

    @Autowired
    UserLogisticMapper userLogisticMapper;

    @Override
    public ResponseResult selectUserLogisticByCondition(Integer pageNum, Integer pageSize, Integer userId, String orderState) {
        List<UserlogisticVo> userlogisticVoList = userLogisticMapper.selectUserLogisticByCondition(userId, orderState);
        if (null != userlogisticVoList && userlogisticVoList.size() > 0) {
            System.out.println("service" + userlogisticVoList.toString());
            PageInfo<UserlogisticVo> userlogisticVoPageInfo = PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(() -> {
                userLogisticMapper.selectUserLogisticByCondition(userId, orderState);
            });
            return resultUtil.getResponseResult(0, "", userlogisticVoList.size(), userlogisticVoPageInfo);
        } else {
            return resultUtil.getResponseResult(2, "没有结果");
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int addOrderIdList(List<Long> orderIdList) {

        int sum = 0;

        for (Long orOneId : orderIdList) {

            DateConversion dateConversion = new DateConversion();

            int affRow = userLogisticMapper.addOrderIdList(orOneId, dateConversion.Data2Sting());
            sum += affRow;
            System.out.println(sum);
        }
        return sum;
    }
}
